//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: socket_gnome1.c 757 2007-05-10 08:19:31Z roger $
//

/*
** $Id: socket_gnome1.c 757 2007-05-10 08:19:31Z roger $
**
** SWin X Auxilliary Objects
** External module -- IM status panel-applet
*/

#include "config.h"

#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <applet-widget.h>

/* Define applet info */
#define APPLET_PKG "unihan_socket_applet"
#define APPLET_VER VERSION

static void sel_recv(GtkWidget *, GtkSelectionData *, guint, gpointer);
static void socket_destroyed(GtkObject *, gpointer);
static gint ignore_1st_click(GtkWidget *, GdkEvent *, gpointer);

static GtkWidget *socket = NULL;
static GtkWidget *applet = NULL;
static GtkWidget *handle = NULL;
static gboolean is_plugged = FALSE;
static guint32 plug_id = 0;

static GtkWidget *
create_socket(void)
{
	GtkWidget *new_socket = gtk_socket_new();
	gtk_signal_connect(GTK_OBJECT(new_socket), "selection_received",
			GTK_SIGNAL_FUNC(sel_recv), NULL);
	gtk_signal_connect(GTK_OBJECT(new_socket), "destroy",
			GTK_SIGNAL_FUNC(socket_destroyed), NULL);
	gtk_widget_set_events(new_socket, GDK_BUTTON_PRESS_MASK);
	gtk_widget_show_all(new_socket);
	return new_socket;
}

/* 'inspired' by tasklist.c in gnome-core! */
static gint 
ignore_1st_click(GtkWidget *w, GdkEvent *e, gpointer data)
{
	GdkEventButton *be = (GdkEventButton *)e;

	if ((e->type == GDK_BUTTON_PRESS || e->type == GDK_BUTTON_RELEASE)
	    && be->button == 1 && be->window != socket->window)
		be->button = 2;
	
	return FALSE;
}

static void
socket_destroyed(GtkObject *o, gpointer data)
{
	is_plugged = FALSE;
	socket = create_socket();
	gtk_container_add(GTK_CONTAINER(handle), socket);
	//applet_widget_add(APPLET_WIDGET(applet), socket);
}

static gint
check_is_plugged(gpointer data)
{
	if (!is_plugged) {
		gtk_selection_convert(socket,
			gdk_atom_intern("_UNIHAN_STATUS", FALSE),
			gdk_atom_intern("_PLUG_ID", FALSE),
			GDK_CURRENT_TIME);
	} else {
		// check if window still exists
		GdkWindow *dummy;
		gdk_error_trap_push();
		dummy = gdk_window_foreign_new(plug_id);
		gdk_error_trap_pop();
		if (!dummy) {
			is_plugged = FALSE;
			g_warning("status window lost");
		}
	}
	
	return TRUE;
}

static void
sel_recv(GtkWidget *w, GtkSelectionData *d, guint time, gpointer data)
{
	guint32 new_plug_id;
	GdkWindow *dummy;

	if (!d) return;
	if (!d->data) return;
	
	new_plug_id = *(guint32*)d->data;

	// Check if window still exists
	gdk_error_trap_push();
	dummy = gdk_window_foreign_new(new_plug_id);
	gdk_error_trap_pop();
	if (!dummy) return;

	plug_id = new_plug_id;
	gtk_socket_steal(GTK_SOCKET(socket), plug_id);
	is_plugged = TRUE;
}

int
main(int argc, char *argv[])
{
	GtkWidget *controls;
	
	applet_widget_init(APPLET_PKG, APPLET_VER, 
			   argc, argv, NULL, 0, NULL);
	applet = applet_widget_new(APPLET_PKG);

	socket = create_socket();

	handle = gtk_handle_box_new();
	gtk_signal_connect(GTK_OBJECT(handle), "event",
			   GTK_SIGNAL_FUNC(ignore_1st_click), NULL);
	gtk_container_add(GTK_CONTAINER(handle), socket);
	applet_widget_add(APPLET_WIDGET(applet), handle);
	gtk_widget_show_all(applet);

	gtk_selection_convert(socket,
			gdk_atom_intern("_UNIHAN_STATUS", FALSE),
			gdk_atom_intern("_PLUG_ID", FALSE),
			GDK_CURRENT_TIME);

	// check every 5 seconds
	gtk_timeout_add(5000, check_is_plugged, NULL);
	
	applet_widget_gtk_main();

	return 0;
}

// vi:ts=4:nowrap:cin:expandtab
